package io.zhifei.modules.sys.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.zhifei.common.utils.PageUtils;
import io.zhifei.common.utils.ToolUtils;
import io.zhifei.modules.app.login.entity.Banner;
import io.zhifei.modules.sys.system.dao.BannerDao;
import io.zhifei.modules.sys.system.form.BannerParam;
import io.zhifei.modules.sys.system.form.BannerResult;
import io.zhifei.modules.sys.system.service.BannerService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * banner 服务实现类
 * </p>
 *
 * @author duke
 * @since 2021-05-21
 */
@Service
public class BannerServiceImpl extends ServiceImpl<BannerDao, Banner> implements BannerService {

    @Override
    public void add(BannerParam param){
        Banner entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BannerParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BannerParam param){
        Banner oldEntity = getOldEntity(param);
        Banner newEntity = getEntity(param);
        ToolUtils.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BannerResult findBySpec(BannerParam param){
        return this.baseMapper.getDetail(param);
    }

    @Override
    public List<BannerResult> findListBySpec(BannerParam param){
        return this.baseMapper.getList(param);
    }

    @Override
    public PageUtils findPageBySpec(Page page,BannerParam param){
        IPage pageList = this.baseMapper.getPageList(page,param);
        return new PageUtils(pageList);
    }

    private Serializable getKey(BannerParam param){
        return param.getId();
    }


    private Banner getOldEntity(BannerParam param) {
        return this.getById(getKey(param));
    }

    private Banner getEntity(BannerParam param) {
        Banner entity = new Banner();
        ToolUtils.copyProperties(param, entity);
        return entity;
    }

}
